#include "qpagewidget.h"
#include "qformwidget.h"

QPageWidget * QPageWidget::m_pageWidget=NULL;

QPageWidget::QPageWidget()
{
}

QPageWidget* QPageWidget::pageWidget()
{
    if(m_pageWidget==NULL)
    {
        m_pageWidget=new QPageWidget();
    }
    return m_pageWidget;
}

void QPageWidget::releasePageWidget()
{
    if(m_pageWidget!=NULL)
    {
        delete m_pageWidget;
        m_pageWidget=NULL;
    }
}

void QPageWidget::insertPage(const QString &name, QFormWidget *page)
{
    connect(page,SIGNAL(destroyed()),this,SLOT(pageDestroyed()));
    m_pages.insert(name,page);
}

void QPageWidget::removePage(const QString &name)
{
    m_pages.remove(name);
}

void QPageWidget::pageDestroyed()
{
    QFormWidget* form=(QFormWidget*)sender();
    removePage(form->name());
}

QStringList QPageWidget::pages()
{
    return m_pages.keys();
}

tagFormInfo QPageWidget::pageMethod(const QString &name)
{
    QFormWidget *f=m_pages.value(name);

    QStringList list;
    list<<"deleteLater()"<<"_q_reregisterTimers(void*)"<<"destroyed(QObject*)"<<"destroyed()"
       <<"exec(QString,QString)"<<"dataChanged(QString,QVariant)"<<"initGlobalObject()";
    tagFormInfo info;
    if(f!=NULL)
    {
        info.m_name=name;
        QObject* o=((QObject*)f->property("host").toInt());
        if(o!=NULL)
        {
            int c=o->metaObject()->methodCount();
            for(int i=0;i<c;i++)
            {
                QMetaMethod m=o->metaObject()->method(i);
                if(m.methodType()==QMetaMethod::Slot || m.methodType()==QMetaMethod::Signal)
                {
                    if(list.indexOf(m.signature())<0)
                    {
                        tagFuncInfo in;
                        in.m_method=m;
                        info.m_funcInfo.append(in);
                    }
                }
            }
        }

        QWidgetList widList=f->widgets();
        foreach(QWidget* wid,widList)
        {
            tagWidgetInfo widInfo;
            widInfo.m_name=wid->objectName();
            QObject* o=((QObject*)wid->property("host").toInt());
            if(o!=NULL)
            {
                int c=o->metaObject()->methodCount();
                for(int i=0;i<c;i++)
                {
                    QMetaMethod m=o->metaObject()->method(i);
                    if(m.methodType()==QMetaMethod::Slot || m.methodType()==QMetaMethod::Signal)
                    {
                        if(list.indexOf(m.signature())<0)
                        {
                            tagFuncInfo in;
                            in.m_method=m;
                            widInfo.m_funcInfo.append(in);
                        }
                    }
                }
                info.m_widgetInfo.append(widInfo);
            }
        }
    }
    return info;
}
